<?php

require_once("simple_html_dom.php");
require_once("class.curl.php");
require_once("class.utilities.php");
require_once("class.resume.php");

class Jobstreet
{
    function doRetrieve($username, $password)
    {
        $userAgent = 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101';
        $cookieFilePath = getcwd().DIRECTORY_SEPARATOR."cookie.txt";
        $debug = false;
        
        return self::doRetrieve2($username, $password, $userAgent, $cookieFilePath, $debug);
    }
    
    function doRetrieve2($username, $password, $userAgent='Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101', $cookieFilePath, $debug)
    {
        $cc = new cURL(true, $cookieFilePath, 'gzip', '', $userAgent);

        $postFieldsArray['login_id'] = $username;
        $postFieldsArray['password'] = $password;
        $postFieldsArray['mode'] = 'login';
        $postFieldsArray['login'] = '1';
        $postFieldsArray['nrfr'] = '1';

        foreach($postFieldsArray as $key=>$value) 
        { 
            $postFieldsString  .= $key.'='.urlencode($value).'&'; 
        }
        rtrim($postFieldsString, '&');

        // login to jobstreet
        $formUrl = "http://myjobstreet.jobstreet.com/home/login.php?nrfr=1";
        $result = $cc->post($formUrl, $postFieldsString); 
        //print_r($result);

        if($result)
        {
            $afterLoginPageDom = str_get_html($result);
            
            // if login failed
            if($errorMessage = $afterLoginPageDom->find('.errorReg', 0)->plaintext)
            {
                //throw new Exception("Failed to login into jobstreet with the following error message: ".$errorMessage);
            }
            
            $cookieArray = Utilities::cookie2Array($cookieFilePath);
            $phpSessionID = $cookieArray['PHPSESSID']['value'];
            
            if(empty($phpSessionID))
            {
                throw new Exception("Failed to retrieve required phpSessionID from cookie");
            }
            
            // query the resume preview page
            /*
            $formUrl = "http://myjobstreet.jobstreet.com/resume/preview-resume.php?x={$phpSessionID}";
            $cc = new cURL(true, $cookieFilePath, 'gzip', '', $userAgent);
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            // analyze the resume
            $html = str_get_html($htmlPageBuffer);
            */
            
            // create resume object
            $myResume = new Resume;

            //
            // query the resume edit contact detail page
            $formUrl = "http://myjobstreet.jobstreet.com/resume/contact.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            // analyze
            $editContactPageDom = str_get_html($htmlPageBuffer);

            $myResume->firstName = $editContactPageDom->find('input[id=first_name]', 0)->value;
            $myResume->lastName = $editContactPageDom->find('input[id=last_name]', 0)->value;
            $myResume->email = $editContactPageDom->find('input[id=email]', 0)->value;
            $myResume->handPhoneCountryCode = $editContactPageDom->find('select[id=handphone_country_code] option[selected]', 0)->value;
            $myResume->handPhoneNo = $editContactPageDom->find('input[id=handphone_no]', 0)->value;
            $myResume->telArea = $editContactPageDom->find('input[id=tel_area_code]', 0)->value;
            $myResume->telNo = $editContactPageDom->find('input[id=tel_no]', 0)->value;
            $myResume->address1 = $editContactPageDom->find('input[id=address1]', 0)->value;
            $myResume->address2 = $editContactPageDom->find('input[id=address2]', 0)->value;
            $myResume->postcode = $editContactPageDom->find('input[id=postcode]', 0)->value;
            $myResume->country = trim($editContactPageDom->find('select[id=country_code] option[selected]', 0)->plaintext);
            $myResume->state = trim($editContactPageDom->find('select[id=state_code] option[selected]', 0)->plaintext);
            $myResume->city = $editContactPageDom->find('input[id=city]', 0)->value;


            //
            // query the resume edit personal particular page
            $formUrl = "http://myjobstreet.jobstreet.com/resume/personal.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            // analyze
            $editPersonalPageDom = str_get_html($htmlPageBuffer);

            $myResume->dobDay = $editPersonalPageDom->find('select[id=dob_day] option[selected]', 0)->value;
            $myResume->dobMonth = $editPersonalPageDom->find('select[id=dob_month] option[selected]', 0)->value;
            $myResume->dobYear = $editPersonalPageDom->find('input[id=dob_year]', 0)->value;
            $myResume->dateOfBirth = "{$myResume->dobYear}-{$myResume->dobMonth}-{$myResume->dobDay}";
            $myResume->age = date("Y") - $myResume->dobYear;

            foreach($editPersonalPageDom->find('input[name=gender_code]') as $genderCode)
            {
                if($genderCode->checked)
                {
                    $genderCode->value == 1 ? $myResume->gender = 'Male' : $myResume->gender = 'Female';
                }
            }

            foreach($editPersonalPageDom->find('input[name=marital_status_code]') as $maritalStatusCode)
            {
                if($maritalStatusCode->checked)
                {
                    $maritalStatusCode->value == 1 ? $myResume->maritalStatus = 'Single' : $myResume->maritalStatus = 'Married';
                }
            }

            $myResume->nationality = trim($editPersonalPageDom->find('select[name=nationality_code] option[selected]', 0)->plaintext);

            foreach($editPersonalPageDom->find('input[id=permanent_residence]') as $pr)
            {
                if(strstr($pr->outertext, "checked"))
                {
                    $myResume->prInCountries[] = trim($pr->parent()->plaintext);
                }
            }

            //
            // query the resume edit Job Preferences page
            $formUrl = "http://myjobstreet.jobstreet.com/resume/job-preference.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            // analyze
            $editPreferencePageDom = str_get_html($htmlPageBuffer);

            foreach($editPreferencePageDom->find('input[name=employment_type[]]') as $employmentType)
            {
                if($employmentType->checked)
                {
                    switch(trim($employmentType->parent()->plaintext))
                    {
                        case 'Full Time':
                        {
                            $myResume->preferredJobType_FullTime = true;
                            break;
                        }
                        case 'Temporary':
                        {
                            $myResume->preferredJobType_Temporary = true;
                            break;
                        }
                        case 'Part Time':
                        {
                            $myResume->preferredJobType_PartTime = true;
                            break;
                        }
                        case 'Internship':
                        {
                            $myResume->preferredJobType_Internship = true;
                            break;
                        }
                        case 'Contract':
                        {
                            $myResume->preferredJobType_Contract = true;
                            break;
                        }
                    }
                }
            }

            $myResume->expectedMonthlySalaryCurrency = trim($editPreferencePageDom->find('select[id=expected_salary_currency] option[selected]', 0)->plaintext);
            $myResume->expectedMonthlySalaryAmount = $editPreferencePageDom->find('input[id=expected_salary]', 0)->value;

            foreach($editPreferencePageDom->find('input[name=travel_status]') as $travelStatus)
            {
                if($travelStatus->checked)
                {
                    $travelStatus->value == 3 ? $myResume->willingToTravel = true : $myResume->willingToTravel = false;
                }
            }

            foreach($editPreferencePageDom->find('input[name=work_relocation]') as $workRelocation)
            {
                if($workRelocation->checked)
                {
                    $workRelocation->value == 1 ? $myResume->willingToRelocate = true : $myResume->willingToRelocate = false;
                }
            }



            //
            // query the resume edit additional info page
            $formUrl = "myjobstreet.jobstreet.com/resume/additional-info.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            // analyze
            $editAddInfoPageDom = str_get_html($htmlPageBuffer);

            $myResume->additionalInfo = $editAddInfoPageDom->find('textarea[name=text_resume]', 0)->innertext;


            foreach($editAddInfoPageDom->find('input[name=available_status_code]') as $availableStatusCode)
            {
                if($availableStatusCode->checked)
                {
                    
                    switch($availableStatusCode->value)
                    {
                        case '1':
                        {
                            $myResume->availability = "I am not actively looking for a job now.";
                            break;
                        }
                        case '2':
                        {
                            $x = trim($editAddInfoPageDom->find('select[name=available_notice] option[selected]', 0)->value);
                            $myResume->availability = "I can start work after {$x} month(s) of notice period.";
                            break;
                        }
                        case '3':
                        {
                            $x = trim($editAddInfoPageDom->find('select[name=aday] option[selected]', 0)->value);
                            $y .= trim($editAddInfoPageDom->find('select[name=amonth] option[selected]', 0)->value);
                            $z .= trim($editAddInfoPageDom->find('select[name=ayear] option[selected]', 0)->value);
                            $myResume->availability = "I can start work after {$z}-{$y}-{$x}.";
                            break;
                        }
                    }
                }
            }

            foreach($editAddInfoPageDom->find('input[name=own_transport_code]') as $ownTransportCode)
            {

                if($ownTransportCode->checked)
                {
                    $ownTransportCode->value == 1 ? $myResume->possessOwnTransport = true : $myResume->possessOwnTransport = false;
                }
            }



            //
            // query the resume edit Skills page
            $formUrl = "myjobstreet.jobstreet.com/resume/skills.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            // analyze
            $editSkillPageDom = str_get_html($htmlPageBuffer);

            $skillsCounter = 0;
            $skillIds = $editSkillPageDom->find('input[name=resume_skill_id[]]');
            $skillDescs = $editSkillPageDom->find('input[name=skill_desc[]]');
            $yoes = $editSkillPageDom->find('select[name=yoe[]] option[selected]');
            $proficiencies = $editSkillPageDom->find('select[name=proficiency[]] option[selected]');

            if($skillIds)
            {
                foreach($skillIds as $skillId)
                {
                    $skill = new Skill;
                    $skill->id = $skillId->value;
                    $skill->title = $skillDescs[$skillsCounter]->value;
                    $skill->yearsOfExperience = trim($yoes[$skillsCounter]->plaintext);
                    $skill->proficiency = trim($proficiencies[$skillsCounter]->plaintext);
                    
                    $myResume->skills[] = $skill;
                    $skillsCounter++;
                }
            }


            //
            // query the resume edit languages page
            $formUrl = "myjobstreet.jobstreet.com/resume/languages.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            // analyze
            $editLanguagePageDom = str_get_html($htmlPageBuffer);

            $languagesCounter = 0;
            $languageIds = $editLanguagePageDom->find('input[name=resume_language_id[]]');
            $languageCodes = $editLanguagePageDom->find('select[name=language_code[]] option[selected]');
            $writtenLevels = $editLanguagePageDom->find('select[name=written_level[]] option[selected]');
            $spokenLevels = $editLanguagePageDom->find('select[name=spoken_level[]] option[selected]');

            if($languageIds)
            {
                foreach($languageIds as $languageId)
                {
                    $language = new Language;
                    $language->id = $languageId->value;
                    $language->language = trim($languageCodes[$languagesCounter]->plaintext);
                    $language->writtenRating = trim($writtenLevels[$languagesCounter]->plaintext);
                    $language->spokenRating = trim($spokenLevels[$languagesCounter]->plaintext);
                    
                    $myResume->languages[] = $language;
                    $languagesCounter++;
                }
            }


            //
            // query the resume edit references page
            $formUrl = "myjobstreet.jobstreet.com/resume/references.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            // analyze
            $editReferencePageDom = str_get_html($htmlPageBuffer);

            $referencesCounter = 0;
            $referenceIds = $editReferencePageDom->find('input[name=resume_referee_id[]]');
            $referenceNames = $editReferencePageDom->find('input[name=referee_name[]]');
            $relationships = $editReferencePageDom->find('input[name=relationship[]]');
            $emails = $editReferencePageDom->find('input[name=email[]]');
            $positionTitles = $editReferencePageDom->find('input[name=position_title[]]');
            $companyNames = $editReferencePageDom->find('input[name=company_name[]]');
            $telCountryCodes = $editReferencePageDom->find('input[name=tel_ctry_code[]]');
            $telAreaCodes = $editReferencePageDom->find('input[name=tel_area_code[]]');
            $telNos = $editReferencePageDom->find('input[name=tel_no[]]');

            if($referenceIds)
            {
                foreach($referenceIds as $referenceId)
                {
                    $reference = new Reference;
                    $reference->id = $referenceId->value;
                    $reference->title = $referenceNames[$referencesCounter]->value;
                    $reference->company = $companyNames[$referencesCounter]->value;
                    $reference->phone = "{$telCountryCodes[$referencesCounter]->value}-{$telAreaCodes[$referencesCounter]->value}-{$telNos[$referencesCounter]->value}";
                    $reference->email = $emails[$referencesCounter]->value;
                    $reference->relationship = $relationships[$referencesCounter]->value;
                    
                    $myResume->references[] = $reference;
                    $referencesCounter++;
                }
            }


            //
            // query the resume edit education/qualification page
            $formUrl = "myjobstreet.jobstreet.com/resume/education.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            if (extension_loaded('tidy')) 
            {
                $tidyConfig = array
                (
                   'indent'         => true,
                   'output-xhtml'   => true,
                   'clean' => true
                );

                $tidy = new tidy;
                $tidy->parseString($htmlPageBuffer, $tidyConfig, 'utf8');
                $tidy->cleanRepair();
                $htmlPageBuffer = tidy_get_output($tidy);
            }

            // analyze
            $editQualificationPageDom = str_get_html($htmlPageBuffer);
            $qualificationsCounter = 0;
            $qualificationsLimit = 5;
            $qualificationIds = $editQualificationPageDom->find('input[name=education_id0]');

            // do a loop
            for($i=0; $i<$qualificationsLimit; $i++)
            {
                // ... to find is a qualification item exists
                if($editQualificationPageDom->find("input[name=education_id{$i}]", 0)->value != '')
                {
                    $qualification = new Qualification;
                    $qualification->id = $editQualificationPageDom->find("input[name=education_id{$i}]", 0)->value;
                    $qualification->institution = $editQualificationPageDom->find("input[name=institute_name{$i}]", 0)->value;
                    /*
                    todo: the following data is not retrievable due to invalid html tag from the source:
                    
                    <select name="institute_country_code0" class='selectField' id="institute_country_code0" 
                    onchange="ValidateCtl(this,'Institute country field is not selected. Please select one.', 1, 'errInstituteCtryMsg1', 'olInstituteCtry1', 'errInstituteCtry1', 'lblInstituteCtry1', 'institute_country_code0')" 
                    /> 
                    <option value="">- Select Institute Country - </option>
                    <option value="3">Afghanistan</option><option value="6">Albania</option><option value="59">Algeria</option><option value="12">American Samoa</option><option value="1">Andorra</option><option value="9">Angola</option><option value="5">Anguilla</option><option value="10">Antarctica</option><option value="4">Antigua and Barbuda</option><option value="11">Argentina</option><option value="7">Armenia</option><option value="15">Aruba</option><option value="14">Australia</option><option value="13">Austria</option><option value="16">Azerbaijan</option><option value="30">Bahamas</option><option value="23">Bahrain</option><option value="19">Bangladesh</option><option value="18">Barbados</option><option value="34">Belarus</option><option value="20">Belgium</option><option value="35">Belize</option><option value="25">Benin</option><option value="26">Bermuda</option><option value="31">Bhutan</option><option value="28">Bolivia</option><option value="17">Bosnia Hercegovina</option><option value="33">Botswana</option><option value="32">Bouvet Island</option><option value="29">Brazil</option><option value="101">British Indian Ocean Territory</option><option value="27">Brunei Darussalam</option><option value="22">Bulgaria</option><option value="21">Burkina Faso</option><option value="24">Burundi</option><option value="111">Cambodia</option><option value="45">Cameroon</option><option value="36">Canada</option><option value="50">Cape Verde</option><option value="118">Cayman Islands</option><option value="39">Central African Republic</option><option value="205">Chad</option><option value="44">Chile</option><option value="46">China</option><option value="51">Christmas Island</option><option value="37">Cocos (Keeling) Islands</option><option value="47">Colombia</option><option value="113">Comoros</option><option value="40">Congo</option><option value="43">Cook Islands</option><option value="48">Costa Rica</option><option value="42">Cote D'ivoire</option><option value="94">Croatia</option><option value="49">Cuba</option><option value="52">Cyprus</option><option value="53">Czech Republic</option><option value="56">Denmark</option><option value="55">Djibouti</option><option value="57">Dominica</option><option value="58">Dominican Republic</option><option value="214">East Timor</option><option value="60">Ecuador</option><option value="62">Egypt</option><option value="201">EL Salvador</option><option value="84">Equatorial Guinea</option><option value="64">Eritrea</option><option value="61">Estonia</option><option value="66">Ethiopia</option><option value="69">Falkland Islands (Malvinas)</option><option value="71">Faroe Islands</option><option value="68">Fiji</option><option value="67">Finland</option><option value="72">France</option><option value="77">French Guiana</option><option value="168">French Polynesia</option><option value="206">French Southern Territories</option><option value="73">Gabon</option><option value="81">Gambia</option><option value="76">Georgia</option><option value="54">Germany</option><option value="78">Ghana</option><option value="79">Gibraltar</option><option value="85">Greece</option><option value="80">Greenland</option><option value="75">Grenada</option><option value="83">Guadeloupe</option><option value="88">Guam</option><option value="87">Guatemala</option><option value="82">Guinea</option><option value="89">Guinea-Bissau</option><option value="90">Guyana</option><option value="95">Haiti</option><option value="92">Heard and Mc Donald Islands</option><option value="93">Honduras</option><option value="91">Hong Kong</option><option value="96">Hungary</option><option value="104">Iceland</option><option value="100">India</option><option value="97">Indonesia</option><option value="103">Iran</option><option value="102">Iraq</option><option value="98">Ireland</option><option value="99">Israel</option><option value="105">Italy</option><option value="106">Jamaica</option><option value="108">Japan</option><option value="107">Jordan</option><option value="119">Kazakhstan</option><option value="109">Kenya</option><option value="112">Kiribati</option><option value="115">Korea (North)</option><option value="116">Korea (South)</option><option value="117">Kuwait</option><option value="110">Kyrgyzstan</option><option value="120">Laos</option><option value="129">Latvia</option><option value="121">Lebanon</option><option value="126">Lesotho</option><option value="125">Liberia</option><option value="130">Libyan Arab Jamahiriya</option><option value="123">Liechtenstein</option><option value="127">Lithuania</option><option value="128">Luxembourg</option><option value="140">Macau</option><option value="136">Macedonia</option><option value="134">Madagascar</option><option value="148">Malawi</option><option value="150" selected="selected">Malaysia</option><option value="147">Maldives</option><option value="137">Mali</option><option value="145">Malta</option><option value="135">Marshall Islands</option><option value="142">Martinique</option><option value="143">Mauritania</option><option value="146">Mauritius</option><option value="236">Mayotte</option><option value="149">Mexico</option><option value="70">Micronesia</option><option value="132">Monaco</option><option value="139">Mongolia</option><option value="144">Montserrat</option><option value="131">Morocco</option><option value="151">Mozambique</option><option value="138">Myanmar</option><option value="152">Nambia</option><option value="161">Nauru</option><option value="160">Nepal</option><option value="158">Netherlands</option><option value="8">Netherlands Antilles</option><option value="153">New Caledonia</option><option value="163">New Zealand</option><option value="157">Nicaragua</option><option value="154">Niger</option><option value="156">Nigeria</option><option value="162">Niue</option><option value="155">Norfolk Island</option><option value="141">Northern Mariana Islands</option><option value="159">Norway</option><option value="164">Oman</option><option value="165">Others</option><option value="171">Pakistan</option><option value="178">Palau</option><option value="176">Palestinian Territory, Occupied</option><option value="166">Panama</option><option value="169">Papua New Guinea</option><option value="179">Paraguay</option><option value="167">Peru</option><option value="170">Philippines</option><option value="174">Pitcairn</option><option value="172">Poland</option><option value="177">Portugal</option><option value="175">Puerto Rico</option><option value="180">Qatar</option><option value="133">Republic Of Moldova</option><option value="181">Reunion</option><option value="182">Romania</option><option value="183">Russia</option><option value="184">Rwanda</option><option value="114">Saint Kitts And Nevis</option><option value="122">Saint Lucia</option><option value="227">Saint Vincent and The Grenadines</option><option value="234">Samoa</option><option value="196">San Marino</option><option value="200">Sao Tome and Principe</option><option value="185">Saudi Arabia</option><option value="197">Senegal</option><option value="187">Seychelles</option><option value="195">Sierra Leone</option><option value="190">Singapore</option><option value="194">Slovakia</option><option value="192">Slovenia</option><option value="186">Solomon Islands</option><option value="198">Somalia</option><option value="238">South Africa</option><option value="86">South Georgia And South Sandwich Islands</option><option value="65">Spain</option><option value="124">Sri Lanka</option><option value="191">St. Helena</option><option value="173">St. Pierre and Miquelon</option><option value="188">Sudan</option><option value="199">Suriname</option><option value="193">Svalbard and Jan Mayen Islands</option><option value="203">Swaziland</option><option value="189">Sweden</option><option value="41">Switzerland</option><option value="202">Syrian Arab Republic</option><option value="218">Taiwan</option><option value="209">Tajikistan</option><option value="219">Tanzania</option><option value="208">Thailand</option><option value="207">TOGO</option><option value="210">Tokelau</option><option value="213">Tonga</option><option value="216">Trinidad and Tobago</option><option value="212">Tunisia</option><option value="215">Turkey</option><option value="211">Turkmenistan</option><option value="204">Turks and Caicos Islands</option><option value="217">Tuvalu</option><option value="221">Uganda</option><option value="220">Ukraine</option><option value="2">United Arab Emirates</option><option value="74">United Kingdom</option><option value="223">United States</option><option value="222">United States Minor Outlying Islands</option><option value="224">Uruguay</option><option value="225">Uzbekistan</option><option value="232">Vanuatu</option><option value="226">Vatican City State (Holy See)</option><option value="228">Venezuela</option><option value="231">Vietnam</option><option value="229">Virgin Islands (British)</option><option value="230">Virgin Islands (U.S.)</option><option value="233">Wallis and Futuna Islands</option><option value="63">Western Sahara</option><option value="235">Yemen</option><option value="237">Yugoslavia</option><option value="38">Zaire</option><option value="239">Zambia</option><option value="240">Zimbabwe</option>

                    </select>

                    */
                    $qualification->institutionCountry = trim($editQualificationPageDom->find("select[name=institute_country_code{$i}] option[selected]", 0)->plaintext);
                    $qualification->qualificationLevel = trim($editQualificationPageDom->find("select[name=qualification_code{$i}] option[selected]", 0)->plaintext);
                    $qualification->fieldOfStudy = trim($editQualificationPageDom->find("select[name=field_of_study_code{$i}] option[selected]", 0)->plaintext);
                    $qualification->graduationYear = trim($editQualificationPageDom->find("select[name=graduation_year{$i}] option[selected]", 0)->plaintext);
                    $qualification->major = $editQualificationPageDom->find("input[name=major{$i}]", 0)->value;
                    $qualification->grade = trim($editQualificationPageDom->find("select[name=average_grade{$i}] option[selected]", 0)->plaintext);
                    
                    $myResume->qualifications[] = $qualification;
                    $qualificationsCounter++;
                }
            }

            // certification
            $certificatesCounter = 0;
            $certificateIds = $editQualificationPageDom->find('input[name=certification_id[]]');
            $certificateNames = $editQualificationPageDom->find('input[name=certification_name[]]');
            $certificateYears = $editQualificationPageDom->find('select[name=certification_year[]] option[selected]');

            if($certificateIds)
            {
                foreach($certificateIds as $certificateId)
                {
                    $certificate = new Certificate;
                    $certificate->id = $certificateId->value;
                    $certificate->title = $certificateNames[$certificatesCounter]->value;
                    $certificate->obtainedYear = trim($certificateYears[$certificatesCounter]->plaintext);
                    
                    $myResume->certificates[] = $certificate;
                    $certificatesCounter++;
                }
            }


            //
            // query the resume edit work-experience page
            $formUrl = "myjobstreet.jobstreet.com/resume/work-experience.php?x={$phpSessionID}";
            $htmlPageBuffer = $cc->get($formUrl, ""); 
            $htmlPageBuffer = str_replace("?", "", $htmlPageBuffer);

            if (extension_loaded('tidy')) 
            {
                $tidyConfig = array
                (
                   'indent'         => true,
                   'output-xhtml'   => true
                );

                $tidy = new tidy;
                $tidy->parseString($htmlPageBuffer, $tidyConfig, 'utf8');
                $tidy->cleanRepair();
                $htmlPageBuffer = tidy_get_output($tidy);
            }


            // analyze
            $editExperiencePageDom = str_get_html($htmlPageBuffer);
            foreach($editExperiencePageDom->find('input[name=experience_level_code]') as $experienceLevelCode)
            {
                if($experienceLevelCode->checked)
                {
                    switch($experienceLevelCode->value)
                    {
                        case '1':
                        {
                            $x = trim($editExperiencePageDom->find('select[name=start_work_year] option[selected]', 0)->value);
                            $myResume->experienceLevel = "I have been working since {$x}.";
                            $myResume->startWorkYear = $x;
                            break;
                        }
                        case '2':
                        {
                            $myResume->experienceLevel = "I am a fresh graduate seeking my first job.";
                            break;
                        }
                        case '3':
                        {
                            $myResume->experienceLevel = "I am a student seeking internship or part-time jobs.";
                            break;
                        }
                    }
                }
            }

            // has_exp
            if($editExperiencePageDom->find('input[name=has_exp]', 0)->value == 'y')
            {
                // get number of work experience to loop
                $exprienceNumbers = $editExperiencePageDom->find('div[id=workexp] div[id=number]');
                $exprienceworkExpPoss = $editExperiencePageDom->find('div[id=workexp] div[id=workExpPos]');
                $exprienceDates = $editExperiencePageDom->find('div[id=workexp] div[id=date]');
                $exprienceActions = $editExperiencePageDom->find('div[id=workexp] div[class=colOdd] a[href=#]');
                if($exprienceNumbers && $exprienceActions)
                {
                    $experiencesCounter = 0;
                    foreach($exprienceNumbers as $exprienceNumber)
                    {
                        $number = $exprienceNumbers[$experiencesCounter]->plaintext;
                        $workExpPos = $exprienceworkExpPoss[$experiencesCounter]->plaintext;
                        $date = $exprienceDates[$experiencesCounter]->plaintext;
                        // times 2 bcs we have both edit and delete link
                        $tmpWes = explode(",", $exprienceActions[$experiencesCounter*2]->onclick);
                        $we = trim(str_replace(array("'", ")"), "", $tmpWes[2]));
                        
                        $editUrl = "myjobstreet.jobstreet.com/resume/edit-work-experience.php?x={$phpSessionID}&action=edit&type=we&we={$we}";
                        $htmlPageBuffer2 = null;
                        
                        // with the editUrl we have, it's time to go digg for more
                        $htmlPageBuffer2 = $cc->get($editUrl, ""); 
                        $htmlPageBuffer2 = str_replace("?", "", $htmlPageBuffer2);
                                    
                        $editExperiencePageDom2 = str_get_html($htmlPageBuffer2);
                        
                        $experience = new Experience;
                        $experience->id = $editExperiencePageDom2->find("input[name=resume_work_experience_id]", 0)->value;
                        $experience->companyName = $editExperiencePageDom2->find("input[name=company_name]", 0)->value;
                        $experience->industry = trim($editExperiencePageDom2->find("select[name=industry_code] option[selected]", 0)->plaintext);
                        $experience->specialization = trim($editExperiencePageDom2->find("select[name=specialization_code] option[selected]", 0)->plaintext);
                        // todo: role is not retrievable here as it is an interactive field triggered by ajax
                        $experience->primaryRole = trim($editExperiencePageDom2->find("select[name=primary_role] option[selected]", 0)->plaintext);
                        $experience->positionLevel = trim($editExperiencePageDom2->find("select[name=position_level_code] option[selected]", 0)->plaintext);
                        $experience->monthlySalaryCurrency = trim($editExperiencePageDom2->find("select[name=salary_currency_code] option[selected]", 0)->plaintext);
                        $experience->workingPeriodFromMonth = trim($editExperiencePageDom2->find("select[name=join_date_month] option[selected]", 0)->plaintext);
                        $experience->workingPeriodFromYear = trim($editExperiencePageDom2->find("select[name=join_date_year] option[selected]", 0)->plaintext);
                        $experience->workingPeriodToMonth = trim($editExperiencePageDom2->find("select[name=left_date_month] option[selected]", 0)->plaintext);
                        $experience->workingPeriodToYear = trim($editExperiencePageDom2->find("select[name=left_date_year] option[selected]", 0)->plaintext);
                        $experience->positionTitle = $editExperiencePageDom2->find("input[name=position_title]", 0)->value;
                        $experience->teamSize = $editExperiencePageDom2->find("input[name=team_size]", 0)->value;
                        $experience->monthlySalaryAmount = $editExperiencePageDom2->find("input[name=salary]", 0)->value;
                        $experience->workDescription = $editExperiencePageDom2->find("textarea[name=work_description]", 0)->plaintext;
                    
                        $myResume->experiences[] = $experience;
                        $experiencesCounter++;
                    }
                }
            }


            //
            // return the resume object
            return $myResume;
        }
    }
}
